package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.quality.domain.req.QualityRippleReq;
import com.ruibang.glass.quality.domain.req.SemiCheckReportReq;
import com.ruibang.glass.quality.domain.resp.SemiCheckDetailData;
import com.ruibang.glass.quality.entity.QualityRipple;
import com.ruibang.glass.quality.entity.SpecsConfig;
import com.ruibang.glass.quality.mapper.QualityRippleMapper;
import com.ruibang.glass.quality.service.QualityRippleService;
import com.ruibang.glass.quality.service.SpecsConfigService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 半成品波纹度信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2024-03-11
 */
@Service
public class QualityRippleServiceImpl extends ServiceImpl<QualityRippleMapper, QualityRipple> implements QualityRippleService {

//    @Autowired
//    private RedisSingleService redisService;

    @Autowired
    private RedisSingleService redisService;

    @Autowired
    private SpecsConfigService specsConfigService;

    @Override
    public void saveInfo(QualityRipple qualityRipple) {
        LocalDateTime now = LocalDateTime.now();
        if(qualityRipple.getTransectTime()==null){
            qualityRipple.setTransectTime(now);
        }
        if(qualityRipple.getRecordTime()==null){
            qualityRipple.setRecordTime(now);
        }
        this.save(qualityRipple);
    }

    @Override
    public void updateInfo(QualityRipple qualityRipple) {
        //更新文件信息
        this.updateById(qualityRipple);
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(QualityRipple::getRippleId, id).set(QualityRipple::getIsDelete, CommonConstant.ONE_STR).update();
    }

    @Override
    public PageResult<QualityRipple> queryPage(PageRequest<QualityRippleReq> pageRequest) {
        final QualityRippleReq param = pageRequest.getParam();
        final LambdaQueryWrapper<QualityRipple> lambda = new QueryWrapper<QualityRipple>().lambda();
        this.buildCondition(lambda,param);
        lambda.orderBy(true,false, QualityRipple::getTransectTime);
        final IPage<QualityRipple> page = this.page(new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize()), lambda);
        List<SpecsConfig> configs = specsConfigService.getSpecsConfigByIds(page.getRecords().stream().map(qt->qt.getCheckRule()).collect(Collectors.toList()));
        Map<String, SpecsConfig> configMap = null;
        if(CollectionUtils.isNotEmpty(configs)){
            configMap = configs.stream().collect(Collectors.toMap(c->c.getSpecsId(),c->c,(c1,c2)->c1));
        }
        Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);

        for (QualityRipple record : page.getRecords()) {
            if(userMap!=null){
                record.setUserName(userMap.get(record.getUserId())==null?"":userMap.get(record.getUserId()).toString());
            }
            if(configMap!=null){
                record.setSpecsConfig(configMap.get(record.getCheckRule()));
            }
        }
        PageResult<QualityRipple> pr = new PageResult(page);
        return pr;
    }

    @Override
    public List<QualityRipple> queryQualityRipple(QualityRippleReq rippleReq) {
        final LambdaQueryWrapper<QualityRipple> lambda = new QueryWrapper<QualityRipple>().lambda();
        this.buildCondition(lambda,rippleReq);
        lambda.orderBy(true,true, QualityRipple::getTransectTime);
        List<QualityRipple> list = this.list(lambda);
        if(CollectionUtils.isNotEmpty(list)){
            Map<Object, Object> productMap = redisService.hmget(RedisKey.PRODUCT_LINE_GROUP_TYPE);
            for (QualityRipple qualityRipple : list) {
                if(productMap!=null){
                    qualityRipple.setProductLineName(productMap.get(qualityRipple.getProductLine())==null?"":productMap.get(qualityRipple.getProductLine()).toString());
                }
            }
        }
        return list;
    }

    @Override
    public List<SemiCheckDetailData> queryRippleCheckData(SemiCheckReportReq reportReq) {
        return baseMapper.queryRippleCheckData(reportReq);
    }

    /**
     * 构造查询条件
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<QualityRipple> lambda, QualityRippleReq param){
        lambda.eq(QualityRipple::getIsDelete, CommonConstant.ZERO_STR);
        if(null == param) return;
        if(StringUtils.isNotBlank(param.getRippleId())){
            lambda.eq(QualityRipple::getRippleId, param.getRippleId());
        }
        if(StringUtils.isNotBlank(param.getProductLine())){
            lambda.eq(QualityRipple::getProductLine, param.getProductLine());
        }
        if(StringUtils.isNotBlank(param.getGlassId())){
            lambda.eq(QualityRipple::getGlassId, param.getGlassId());
        }
        if(StringUtils.isNotBlank(param.getGlassType())){
            lambda.eq(QualityRipple::getGlassType, param.getGlassType());
        }
        if(StringUtils.isNotBlank(param.getGlassModel())){
            lambda.eq(QualityRipple::getGlassModel, param.getGlassModel());
        }
        if(StringUtils.isNotBlank(param.getCheckRule())){
            lambda.eq(QualityRipple::getCheckRule, param.getCheckRule());
        }
        if(StringUtils.isNotBlank(param.getUserId())){
            lambda.eq(QualityRipple::getUserId, param.getUserId());
        }

        if(param.getRecordStartTime()!=null&&param.getRecordEndTime()!=null){
            lambda.ge(QualityRipple::getRecordTime, param.getRecordStartTime());
            lambda.le(QualityRipple::getRecordTime, param.getRecordEndTime());
        }
        if(param.getTransectStartTime()!=null&&param.getTransectEndTime()!=null){
            lambda.ge(QualityRipple::getTransectTime, param.getTransectStartTime());
            lambda.le(QualityRipple::getTransectTime, param.getTransectEndTime());
        }

        if(StringUtils.isNotBlank(param.getTeam())){
            lambda.eq(QualityRipple::getTeam, param.getTeam());
        }
        if(StringUtils.isNotBlank(param.getDecide())){
            lambda.eq(QualityRipple::getDecide, param.getDecide());
        }
    }
}
